<template>
<d2-container>
  <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm"
           style="width: 30%;">
    <el-form-item label="数量" prop="number">
      <el-input type="number" v-model="ruleForm.number"></el-input>
    </el-form-item>

    <el-form-item label="尺寸" prop="size">
      <el-select v-model="ruleForm.size" placeholder="请选择箱子尺寸">
        <el-option v-for="item in ruleForm.packet_category" :label="item" :value="item"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="ruleForm.remark"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>

</d2-container>
</template>

<script>
const axios = require('axios');
import {request} from "@/network/request";

export default {
  name: 'create',
  data() {
    return {
      ruleForm: {
        number: '',
        size: '',
        packet_category:'',
        remark: '无'
      },
      rules: {
        number: [
          {required: true, message: '请输入需要增加的包装盒数量', trigger: 'blur'}
        ],
        size: [
          {required: true, message: '请输入该批产品容量', trigger: 'blur'}
        ],
        remark: [
          {required: true, message: '请输入备注', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      let _this = this;
      //上传数据
      request({
          url: '/packet/largeAddition',
          method: 'post',
          params: {
            number: _this.ruleForm.number,
            size: _this.ruleForm.size,
            remark: _this.ruleForm.remark
          }
        },
        res => {
          alert("添加成功！");
          console.log(res);
        },
        err => {
          alert("添加失败，请重试！");
          console.log(err);
        }
      );
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  created() {
    let _this = this;
    //原材料分类
    request({
        url: 'packetStatistics/query',
        params: {
          key: '',
          pages: '1'
        },
      },
      res => {
        console.log(res.data.records.length);
        _this.ruleForm.packet_category = [];
        for (let i = 0; i < res.data.records.length; i++) {
          _this.ruleForm.packet_category.push(res.data.records[i].size
          ) ;
        }
      },
      err => {
        console.log(err);
      });
  }
}
</script>
